package com.yyfax.ph.progress.supplement;


import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.List;

public class SupplementModel extends Model {

	@Override
	public void onNext(Object obj) {
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case SupplementPresenter.REQUEST_CODE_CANCEL:
				CancelResult cancelResult = cast(obj);
				EventAgent.post(cancelResult);

				break;
			case SupplementPresenter.REQUEST_CODE_SUPPLEMENT:
				SupplementResult supplementResult = cast(obj);
				EventAgent.post(supplementResult);
				break;
		}
	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode) {
			case SupplementPresenter.REQUEST_CODE_CANCEL:
				return service.cancelRecommend(token, (Cancel) params);
			case SupplementPresenter.REQUEST_CODE_SUPPLEMENT:
				return service.supplement(token, (Supplement) params);
		}
		return null;
	}

	private interface Service {
		@POST("apply/deal/cancel")
		Observable<CancelResult> cancelRecommend(@Header("token") String token, @Body Cancel cancel);

		@POST("apply/deal/supplement")
		Observable<SupplementResult> supplement(@Header("token") String token, @Body Supplement supplement);
	}

	public static class Cancel {
		public String rcmdId;
	}

	static class CancelResult extends BaseResultEntity<SingleContentBean> {
	}

	public static class SingleContentBean {
		public String code;
	}

	public static class Supplement {
		String rcmdId;//推荐编号
		List<String> imageIds;//图片id
		String flag;//是否简单补件 0：征信授权；1：专业推荐
		String remark;//备注

		Supplement(String rcmdId, List<String> imageIds) {
			this.rcmdId = rcmdId;
			this.imageIds = imageIds;
		}
	}

	static class SupplementResult extends BaseResultEntity<SingleContentBean> {
	}

}
